package com.wjk.cms.service.impl;

import com.wjk.cms.dao.SysContentRepository;
import com.wjk.cms.pojo.SysContent;
import com.wjk.cms.service.SysContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysContentServiceImpl implements SysContentService {

    @Autowired
    private SysContentRepository sysContentRepository;
    @Autowired
    private EntityManager entityManager;
    @Override
    public List<SysContent> findByCategory(String category, String isHidden) {
        return sysContentRepository.findAll((root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotEmpty(category)) {

//                predicates.add(cb.like(root.get("category"), "%"+category+"%"));
                predicates.add(cb.equal(root.get("category"), category));
            }
            if(StringUtils.isNotBlank(isHidden)) {
                predicates.add(cb.equal(root.get("isHidden"), isHidden));
            }
            return cb.and(predicates.toArray(new Predicate[0]));
        }, Sort.by(Sort.Order.asc("displayOrder"), Sort.Order.desc("id")));
    }


    @Override
    public void updateByCategory(SysContent sysContent) {
        SysContent nowSysContent = sysContentRepository.getById(sysContent.getId());
        if(StringUtils.isNotBlank(sysContent.getBody())){
            nowSysContent.setBody(sysContent.getBody());
        }
        if(StringUtils.isNotBlank(sysContent.getTitle())){
            nowSysContent.setTitle(sysContent.getTitle());
        }
        if(StringUtils.isNotBlank(sysContent.getMstr1())){
            nowSysContent.setMstr1(sysContent.getMstr1());
        }
        if(StringUtils.isNotBlank(sysContent.getMstr2())){
            nowSysContent.setMstr2(sysContent.getMstr2());
        }
        if(StringUtils.isNotBlank(sysContent.getMstr3())){
            nowSysContent.setMstr3(sysContent.getMstr3());
        }
        if(StringUtils.isNotBlank(sysContent.getMstr4())){
            nowSysContent.setMstr4(sysContent.getMstr4());;
        }
        if(StringUtils.isNotBlank(sysContent.getMstr5())){
            nowSysContent.setMstr5(sysContent.getMstr5());
        }

        if(StringUtils.isNotBlank(sysContent.getStr1())){
            nowSysContent.setStr1(sysContent.getStr1());
        }
        if(StringUtils.isNotBlank(sysContent.getStr2())){
            nowSysContent.setStr2(sysContent.getStr2());
        }
        if(StringUtils.isNotBlank(sysContent.getStr3())){
            nowSysContent.setStr3(sysContent.getStr3());
        }
        if(StringUtils.isNotBlank(sysContent.getStr4())){
            nowSysContent.setStr4(sysContent.getStr4());
        }
        if(StringUtils.isNotBlank(sysContent.getStr5())){
            nowSysContent.setStr5(sysContent.getStr5());
        }

        if(StringUtils.isNotBlank(sysContent.getPic1())){
            nowSysContent.setPic1(sysContent.getPic1());
        }
        if(StringUtils.isNotBlank(sysContent.getPic2())){
            nowSysContent.setPic2(sysContent.getPic2());
        }
        if(StringUtils.isNotBlank(sysContent.getPic3())){
            nowSysContent.setPic3(sysContent.getPic3());
        }
        if(StringUtils.isNotBlank(sysContent.getPic4())){
            nowSysContent.setPic4(sysContent.getPic4());
        }
        if(StringUtils.isNotBlank(sysContent.getPic5())){
            nowSysContent.setPic5(sysContent.getPic5());
        }

        if(StringUtils.isNotBlank(sysContent.getIsHidden())){
            nowSysContent.setIsHidden(sysContent.getIsHidden());
        }


       sysContentRepository.save(nowSysContent);
    }

    @Override
    public SysContent findById(Long contentId) {
        return sysContentRepository.findById(contentId).orElse(null);
    }

    @Override
    public void save(SysContent content) {
        sysContentRepository.save(content);
    }

    public void addSysContent(SysContent sysContent){
        sysContentRepository.save(sysContent);
    }

    @Override
    public void deleteById(Long id) {
        sysContentRepository.deleteById(id);
    }

    ;


}
